//
//  BKEmotionKeyBoard.swift
//  微博
//
//  Created by mac on 16/8/19.
//  Copyright © 2016年 xbk. All rights reserved.
//

import UIKit

private let EmotionCollectionViewCellIdentifier = "EmotionCollectionViewCellIdentifier"

class BKEmotionKeyBoard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //  MARK: -- 懒加载创建控件
    private lazy var toolBar:BKEmotionToolBar = {
    
        let toolBar = BKEmotionToolBar(frame:CGRectZero)
        return toolBar
    }()
    
    private lazy var emotionCollectionView:UICollectionView = {
        
        let flowLayout = UICollectionViewFlowLayout()
        
        flowLayout.scrollDirection = .Horizontal
    
        let view = UICollectionView(frame: CGRectZero, collectionViewLayout: flowLayout)
        view.dataSource = self
        //注册cell
        view.registerClass(BKEmotionViewCell.self, forCellWithReuseIdentifier: EmotionCollectionViewCellIdentifier)
        
        view.showsHorizontalScrollIndicator = false
        view.showsVerticalScrollIndicator = false
        view.bounces = false
        view.pagingEnabled = true
        
        return view
    }()

    
    //添加控件,设置约束
    private func setupUI() {
    
        addSubview(toolBar)
        addSubview(emotionCollectionView)
        
        toolBar.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.height.equalTo(35)
        }
        
        emotionCollectionView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self)
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        toolBar.didSelectButtonClosure = { [weak self] (type:BKEmoticonToolBarButtonType) in
        
            switch type {
            
            case .Normal:
                print("默认")
            case.Emoji:
                print("Emoji")
            case.LXH:
                print("LXH")
            
            }
        }
    }
    
    //设置子控件的布局方式
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let flowLayout = emotionCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        
        flowLayout.itemSize = emotionCollectionView.size
        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
    }
}

extension BKEmotionKeyBoard:UICollectionViewDataSource{

    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
        return 3
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return 2
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(EmotionCollectionViewCellIdentifier, forIndexPath: indexPath) as! BKEmotionViewCell
        
        cell.indexPath = indexPath
        cell.backgroundColor = RandomColor()
        return cell
    }
}

